<?php

class ScoreController extends BaseController
{
    private $req;
    public function init()
    {
        $this->req = $this->getRequest();
        if ($this->req->isXMLHttpRequest()) {
            Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->setNoRender(true);
            $layout = Zend_Layout::getMvcInstance();
            if ($layout instanceof Zend_Layout) {
                $layout->disableLayout();
            }
        }
        $this->view->breadcrumb = array(array(
            'url' => '/score/index',
            'name' => '积分排行',
        ));
    }


    /**
     * 积分排行
     */
    public function indexAction()
    {
        $size = 10;
        $now = $this->req->get('p') ? $this->req->get('p') : 1;//当前页数
        $reset = new \Base\Rest();
        $score_type = \Orm\UserAccount\UserAccountClass::SCORE_TYPE_TOTAL;
        $week_type = \Orm\UserAccount\UserAccountClass::SCORE_TYPE_TOTAL;
        $req['type'] = $score_type;
        $req['week_type'] = $week_type;
        $req['limit'] = $size;
        $req['offset'] = ($now - 1) * $size;
        $rest_res = $reset->get('Score', false , $req);
        $page = new Page($rest_res->getResponse()['total'], $size, $now, '/score/index', 2);
        $this->view->size = $size;
        $this->view->now = $now;
        $this->view->pageStr = $page->showPage();
        if($rest_res->isSuccess()){
            $this->view->score_list = $rest_res->getResponse()['list'];
        }
    }

    /**
     * 积分排行
     */
    public function oneWeekScoreAction()
    {
        $size = 10;
        $now = $this->req->get('p') ? $this->req->get('p') : 1;//当前页数
        $reset = new \Base\Rest();
        $score_type = \Orm\UserAccount\UserAccountClass::SCORE_TYPE_WEEK;
        $week_type = \Orm\UserAccount\UserAccountClass::ONE_WEEK_SCORE;
        $req['type'] = $score_type;
        $req['week_type'] = $week_type;
        $req['limit'] = $size;
        $req['offset'] = ($now - 1) * $size;
        $rest_res = $reset->get('Score', false , $req);
        $page = new Page($rest_res->getResponse()['total'], $size, $now, '/score/one-week-score', 2);
        $this->view->size = $size;
        $this->view->now = $now;
        $this->view->pageStr = $page->showPage();
        if($rest_res->isSuccess()){
            $this->view->score_list = $rest_res->getResponse()['list'];
        }
    }
    /**
     * 积分排行
     */
    public function twoWeekScoreAction()
    {
        $size = 10;
        $now = $this->req->get('p') ? $this->req->get('p') : 1;//当前页数
        $reset = new \Base\Rest();
        $score_type = \Orm\UserAccount\UserAccountClass::SCORE_TYPE_WEEK;
        $week_type = \Orm\UserAccount\UserAccountClass::TWO_WEEK_SCORE;
        $req['type'] = $score_type;
        $req['week_type'] = $week_type;
        $req['limit'] = $size;
        $req['offset'] = ($now - 1) * $size;
        $rest_res = $reset->get('Score', false , $req);
        $page = new Page($rest_res->getResponse()['total'], $size, $now, '/score/two-week-score', 2);
        $this->view->size = $size;
        $this->view->now = $now;
        $this->view->pageStr = $page->showPage();
        if($rest_res->isSuccess()){
            $this->view->score_list = $rest_res->getResponse()['list'];
        }
    }
    /**
     * 积分排行
     */
    public function threeWeekScoreAction()
    {
        $size = 10;
        $now = $this->req->get('p') ? $this->req->get('p') : 1;//当前页数
        $reset = new \Base\Rest();
        $score_type = \Orm\UserAccount\UserAccountClass::SCORE_TYPE_WEEK;
        $week_type = \Orm\UserAccount\UserAccountClass::THREE_WEEK_SCORE;
        $req['type'] = $score_type;
        $req['week_type'] = $week_type;
        $req['limit'] = $size;
        $req['offset'] = ($now - 1) * $size;
        $rest_res = $reset->get('Score', false , $req);
        $page = new Page($rest_res->getResponse()['total'], $size, $now, '/score/three-week-score', 2);
        $this->view->size = $size;
        $this->view->now = $now;
        $this->view->pageStr = $page->showPage();
        if($rest_res->isSuccess()){
            $this->view->score_list = $rest_res->getResponse()['list'];
        }
    }

    public function fourWeekScoreAction()
    {
        $size = 10;
        $now = $this->req->get('p') ? $this->req->get('p') : 1;//当前页数
        $reset = new \Base\Rest();
        $score_type = \Orm\UserAccount\UserAccountClass::SCORE_TYPE_WEEK;
        $week_type = \Orm\UserAccount\UserAccountClass::FOUR_WEEK_SCORE;
        $req['type'] = $score_type;
        $req['week_type'] = $week_type;
        $req['limit'] = $size;
        $req['offset'] = ($now - 1) * $size;
        $rest_res = $reset->get('Score', false , $req);
        $page = new Page($rest_res->getResponse()['total'], $size, $now, '/score/four-week-score', 2);
        $this->view->size = $size;
        $this->view->now = $now;
        $this->view->pageStr = $page->showPage();
        if($rest_res->isSuccess()){
            $this->view->score_list = $rest_res->getResponse()['list'];
        }
    }

    /**
     * 根据积分排行获取积分中奖纪录
     */
    public function userAction(){
        try {
            // 检查请求
            if (!$this->req->isGet()) {
                return Response::Error(0, '');
            }

            $searchArr = array();
            $type = $this->req->get('type');
            $week_type = $this->req->get('week_type');
            $this->view->type = $type;
            $this->view->week_type = $week_type != '' ? $week_type : 1;

            $where =" a.`is_del` = 0 ";
            if($type !=''){
                if($type == '1'){
                    $searchArr['type'] = $type;
                    $where .=" AND a.`type` = ".$type;
                    if($week_type != ''){
                        $searchArr['week_type'] = $week_type;
                        $where .=" AND a.`week_type` = ".$week_type;
                    }
                }elseif ($type == '2'){
                    $searchArr['type'] = $type;
                    $where .=" AND a.`type` = ".$type;
                }
            }else{
                $searchArr['type'] = 1;
                $where .=" AND a.`type` =1 ";
                $searchArr['week_type'] = 1;
                $where .=" AND a.`week_type` = 1";
            }
            // 获取每页显示条数
            $size = Page::ValidatePageSize((int)$this->req->get('pageSize'), '/score/user');
            // 获取当前页数
            $now = $this->req->get('p') ? $this->req->get('p') : 1;
            $offset = ($now - 1) * $size;
            $userRankBiz = new \Biz\UserRank();
            $userRanList= $userRankBiz->getUserRankList($where, $offset, $size);
            $total =  $userRanList['total'];
            $total_page = ceil($total / $size);
            $page = new Page($total, $size, $now, '/score/user?'.$this->makeUrl($searchArr), 2);
            $this->view->size = $size;
            $this->view->now = $now;
            $this->view->page = $page->showPage();
            $this->view->list = $userRanList['list'];
            $this->view->total_page = $total_page;
            $this->view->breadcrumb = array(array(
                'name' => '积分中奖纪录',
            ));
        } catch (Exception $e) {
            return Response::Error($e->getCode(), $e->getMessage());
        }
    }

    public function addLogisticsAction(){
        $id = $this->req->getPost('id');
        $logistics_num = $this->req->getPost('logistics_num');
        $userRankBiz = new \Biz\UserRank();
        $userRank['logistics_num'] = $logistics_num;
        $userRank['id'] = $id;
        $userRank['status'] = 1;//更新为已发货
        $userRankBiz->update($userRank);
        return Response::Success('','发货成功');
    }
}